package com.glorysoft.knowlespda.ui.track.view

import android.graphics.Color
import android.os.Bundle
import android.view.View
import com.glorysoft.knowlespda.BR
import com.glorysoft.knowlespda.R
import com.glorysoft.knowlespda.base.BaseActivity
import com.glorysoft.knowlespda.databinding.ActivityTrackBinding
import com.glorysoft.knowlespda.entity.ui.ToastMessageBean
import com.glorysoft.knowlespda.ui.jobinfo.view.JobInfoActivity
import com.glorysoft.knowlespda.ui.track.viewmodel.TrackViewModel
import com.glorysoft.knowlespda.utils.FreeApi_DialogUtils
import com.glorysoft.knowlespda.utils.MyUtils.setEditTextInput
import kotlinx.android.synthetic.main.activity_title.*
import kotlinx.android.synthetic.main.activity_track.*

/**
 * FileName: HomeActivity
 * Author: XJH
 * Date: 2021/7/12 17:19
 * Description:
 * History:
 */
class TracksferActivity : BaseActivity<TrackViewModel, ActivityTrackBinding>(),
    View.OnClickListener {
    override fun layoutId(): Int {
        return R.layout.activity_track
    }

    override fun initVariableId(): Int {
        return BR.trackViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        val stringExtra = intent.getStringExtra("DATA")
        if (stringExtra != null && stringExtra.isNotEmpty()) {
            etMachineID.setText(stringExtra)
        }
        etMachineID.requestFocus()
        setTitle(this.getString(R.string.transfer))
        rlTitle.setBackgroundColor(Color.GREEN)
        setEditTextInput(etMachineID, this)
        setEditTextInput(etMagazineNo, this)
        initEdit(etMachineID, object : EditTextListener {
            override fun onTextChangeListener(s: String) {
//                if (etMagazineNo.text.toString().isNotEmpty()) {
//                    viewModel.getSiteInformation(s, etMagazineNo.text.toString())
//                }
            }

        })
        initEdit(etMagazineNo, object : EditTextListener {
            override fun onTextChangeListener(s: String) {
//                if (etMachineID.text.toString().isNotEmpty()) {
//                    viewModel.getSiteInformation(etMachineID.text.toString(), s)
//                }
            }

        })

        tvPartial.setOnClickListener(this)
        tvTrack.setOnClickListener(this)
        tvDetail.setOnClickListener(this)

        tvTrack.text = this.getString(R.string.transfer)
        llLastJobLayout.visibility = View.VISIBLE
        llPartLayout.visibility = View.GONE
        llBoardsLayout.visibility = View.GONE
        llBoardsNumLayout.visibility = View.GONE
        rvBoards.visibility = View.GONE
        tvDetail.visibility = View.GONE
        tvTrack.visibility = View.VISIBLE
    }

    override fun initData() {
        viewModel.defUI.callObserve.observe(this) {
            when (it.type) {
                0 -> {
                    mBinding.setVariable(BR.trackViewModel, viewModel)
                    tvTrack.visibility = View.VISIBLE
                }
                1 -> {
                    etMachineID.setText("")
                    etMagazineNo.setText("")
                }
            }
        }

        viewModel.defUI.startActivity.observe(this) {
            val tipDialog = FreeApi_DialogUtils.getConfirmDialog(this, this.getString(R.string.confirm), this.getString(R.string.operation_succeeded),this.getString(R.string.tips)) {
                finish()
            }
            tipDialog.show(this.supportFragmentManager, "DIALOG")
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tvTrack -> {
                if (etMachineID.text.toString().isNotEmpty() && etMagazineNo.text.toString()
                        .isNotEmpty()
                ) {
                    viewModel.transfer(
                        cbIsLast.isChecked,
                        etMachineID.text.toString(),
                        etMagazineNo.text.toString()
                    )
                } else {
                    viewModel.defUI.toastEvent.value = ToastMessageBean(this.getString(R.string.please_enter_value), false)
                }

            }
            R.id.tvDetail -> {
                if (etMagazineNo.text.toString().isEmpty()) {
                    viewModel.defUI.toastEvent.value =
                        ToastMessageBean(this.getString(R.string.please_enter_magazine_no), false)
                    return
                }
                val bundle = Bundle()
                bundle.putString("MAGAZINENO", etMagazineNo.text.toString())
                startActivity(JobInfoActivity::class.java, bundle)
            }
            R.id.tvPartial -> {
                if (etMachineID.text.toString().isNotEmpty() && etMagazineNo.text.toString()
                        .isNotEmpty()
                ) {
                    viewModel.showBoardID(etMachineID.text.toString(), etMagazineNo.text.toString())
                } else {
                    viewModel.defUI.toastEvent.value = ToastMessageBean(this.getString(R.string.please_enter_value), false)
                }

            }
        }
    }
}